﻿using SD.Common;
using SD.Infrastructure.Constants;
using SlamDunk.MES.IAppService.DTOs.Outputs.ExecutionContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.MaintenanceContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.PlanContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.QualityContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.StockContext;
using SlamDunk.MES.Presentation.Models.ExportModels;
using SlamDunk.MES.ReportView.Models;

namespace SlamDunk.MES.Presentation.Maps
{
    /// <summary>
    /// 导出模型相关映射
    /// </summary>
    public static class ExportMap
    {
        #region # 入库单导出模型映射 —— static StockInOrderEm ToExportView(this StockInOrderInfo...
        /// <summary>
        /// 入库单导出模型映射
        /// </summary>
        public static StockInOrderEm ToExportView(this StockInOrderInfo stockInOrderInfo)
        {
            StockInOrderEm stockInOrderEm = new StockInOrderEm
            {
                单据编号 = stockInOrderInfo.Number,
                单据名称 = stockInOrderInfo.Name,
                单据来源 = stockInOrderInfo.SourceOrderType.HasValue
                    ? stockInOrderInfo.SourceOrderType.Value.GetEnumMember()
                    : string.Empty,
                制单人 = stockInOrderInfo.CreatorName,
                目标储存位置类型 = stockInOrderInfo.TargetStorageLocationType.GetEnumMember(),
                目标存储位置 = stockInOrderInfo.TargetStorageLocationName,
                是否生成条码 = stockInOrderInfo.GenerateBarcode ? "需生成" : "无",
                创建时间 = stockInOrderInfo.AddedTime.ToString(CommonConstants.TimeFormat),
                审核状态 = stockInOrderInfo.CheckStatus.GetEnumMember()
            };

            return stockInOrderEm;
        }
        #endregion

        #region # 出库单导出模型映射 —— static StockOutOrderEm ToExportView(this StockOutOrderInfo...
        /// <summary>
        /// 出库单导出模型映射
        /// </summary>
        public static StockOutOrderEm ToExportView(this StockOutOrderInfo stockOutOrderInfo)
        {
            StockOutOrderEm stockOutOrderEm = new StockOutOrderEm
            {
                单据编号 = stockOutOrderInfo.Number,
                单据名称 = stockOutOrderInfo.Name,
                源单据类型 = stockOutOrderInfo.SourceOrderType.GetEnumMember(),
                源单据名称 = stockOutOrderInfo.SourceOrderName,
                源储存位置类型 = stockOutOrderInfo.SourceStorageLocationType.GetEnumMember(),
                源储存位置名称 = stockOutOrderInfo.SourceStorageLocationName,
                目标储存位置类型 = stockOutOrderInfo.TargetStorageLocationType.GetEnumMember(),
                目标存储位置 = stockOutOrderInfo.TargetStorageLocationName,
                创建时间 = stockOutOrderInfo.AddedTime.ToString(CommonConstants.TimeFormat),
                制单人 = stockOutOrderInfo.CreatorName,
                审核状态 = stockOutOrderInfo.CheckStatus.GetEnumMember(),
                审核人 = stockOutOrderInfo.CheckerName
            };

            return stockOutOrderEm;
        }
        #endregion

        #region # 库存结转导出模型映射 —— static StockSettlementEm ToExportView(this StockSettlementInfo...
        /// <summary>
        /// 库存结转导出模型映射
        /// </summary>
        public static StockSettlementEm ToExportView(this StockSettlementInfo stockSettlementInfo)
        {
            StockSettlementEm stockSettlementEm = new StockSettlementEm
            {
                储存位置类型 = stockSettlementInfo.StorageLocationType.GetEnumMember(),
                储存位置名称 = stockSettlementInfo.StorageLocationName,
                物料编号 = stockSettlementInfo.MaterialInfo?.Number,
                物料名称 = stockSettlementInfo.MaterialInfo?.Name,
                物料批次 = stockSettlementInfo.MaterialLotInfo?.ProductionBatchNo,
                接收日期 = stockSettlementInfo.MaterialLotInfo?.ReceptionDate.ToString(CommonConstants.DateFormat),
                数量 = stockSettlementInfo.Quantity.ToString("0.00"),
                单位 = stockSettlementInfo.MaterialInfo?.StandardUnitNo,
                结转日期 = stockSettlementInfo.SettledDate.ToString(CommonConstants.DateFormat),
                描述 = stockSettlementInfo.Description
            };

            return stockSettlementEm;
        }
        #endregion

        #region # 物料库存导出模型映射 —— static MaterialStockEm ToExportView(this MaterialStockInfo...
        /// <summary>
        /// 物料库存导出模型映射
        /// </summary>
        public static MaterialStockEm ToExportView(this MaterialStockInfo materialStockInfo)
        {
            MaterialStockEm materialStockEm = new MaterialStockEm
            {
                物料名称 = materialStockInfo.MaterialName,
                供应商 = materialStockInfo.SupplierName,
                生产批号 = materialStockInfo.ProductionBatchNo,
                生产日期 = materialStockInfo.ProducedDate.HasValue
                    ? materialStockInfo.ProducedDate.Value.ToString(CommonConstants.DateFormat)
                    : string.Empty,
                保质期 = materialStockInfo.GuaranteePeriod.HasValue
                    ? materialStockInfo.GuaranteePeriod.Value.Days.ToString()
                    : string.Empty,
                过期日期 = materialStockInfo.ExpiredDate.HasValue
                    ? materialStockInfo.ExpiredDate.Value.ToString(CommonConstants.DateFormat)
                    : string.Empty,
                接收日期 = materialStockInfo.ReceptionDate.HasValue
                    ? materialStockInfo.ReceptionDate.Value.ToString(CommonConstants.DateFormat)
                    : string.Empty,
                规格 = materialStockInfo.Specification,
                物料来源 = materialStockInfo.MaterialSourceType.HasValue
                    ? materialStockInfo.MaterialSourceType.Value.GetEnumMember()
                    : string.Empty,
                储存位置类型 = materialStockInfo.StorageLocationType.GetEnumMember(),
                储存位置名称 = materialStockInfo.StorageLocationName,
                总入库数量 = materialStockInfo.TotalInQuantity.ToString("0.00"),
                总出库数量 = materialStockInfo.TotalOutQuantity.ToString("0.00"),
                库存数量 = materialStockInfo.StockQuantity.ToString("0.00")
            };

            return materialStockEm;
        }
        #endregion

        #region # 出/入库记录导出模型映射 —— static BoundRecordEm ToExportView(this BoundRecordInfo...
        /// <summary>
        /// 出/入库记录导出模型映射
        /// </summary>
        public static BoundRecordEm ToExportView(this BoundRecordInfo boundRecordInfo)
        {
            BoundRecordEm boundRecordEm = new BoundRecordEm
            {
                出入方向类型 = boundRecordInfo.DirectionType.GetEnumMember(),
                储存位置类型 = boundRecordInfo.StorageLocationType.GetEnumMember(),
                储存位置名称 = boundRecordInfo.StorageLocationName,
                库存单据编号 = boundRecordInfo.StockOrderNo,
                物料编号 = boundRecordInfo.MaterialInfo.Number,
                物料名称 = boundRecordInfo.MaterialInfo.Name,
                物料批次 = boundRecordInfo.MaterialLotInfo?.ProductionBatchNo,
                接收日期 = boundRecordInfo.MaterialLotInfo?.ReceptionDate.ToString(CommonConstants.DateFormat),
                数量 = boundRecordInfo.Quantity.ToString("0.00"),
                单位 = boundRecordInfo.MaterialInfo.StandardUnitNo,
                出入时间 = boundRecordInfo.AddedTime.ToString("0.00")
            };

            return boundRecordEm;
        }
        #endregion

        #region # 销售订单导出模型映射 —— static SaleOrderEm ToExportView(this SaleOrderInfo...
        /// <summary>
        /// 销售订单导出模型映射
        /// </summary>
        public static SaleOrderEm ToExportView(this SaleOrderInfo saleOrderInfo)
        {
            SaleOrderEm saleOrderEm = new SaleOrderEm
            {
                订单编号 = saleOrderInfo.Number,
                订单名称 = saleOrderInfo.Name,
                客户 = saleOrderInfo.CustomerInfo?.Name,
                下单日期 = saleOrderInfo.IssuedDate.ToString(CommonConstants.DateFormat),
                交付日期 = saleOrderInfo.DeliveryDate.ToString(CommonConstants.DateFormat),
                制单人 = saleOrderInfo.CreatorName,
                审核人 = saleOrderInfo.CheckerName,
                审核状态 = saleOrderInfo.CheckStatus.GetEnumMember(),
                审核时间 = saleOrderInfo.CheckedTime.ToString(),
                创建时间 = saleOrderInfo.AddedTime.ToString(CommonConstants.TimeFormat)
            };

            return saleOrderEm;
        }
        #endregion

        #region # 生产计划导出模型映射 —— static ProductionPlanEm ToExportView(this ProductionPlanInfo...
        /// <summary>
        /// 生产计划导出模型映射
        /// </summary>
        public static ProductionPlanEm ToExportView(this ProductionPlanInfo productionPlanInfo)
        {
            ProductionPlanEm productionPlanEm = new ProductionPlanEm
            {
                计划编号 = productionPlanInfo.Number,
                计划名称 = productionPlanInfo.Name,
                产品 = productionPlanInfo.ProductInfo?.Name,
                物料清单 = productionPlanInfo.BomInfo?.Name,
                生产批号 = productionPlanInfo.ProductionBatchNo,
                工厂模型 = productionPlanInfo.FacilityInfo?.Name,
                预期毛产量 = productionPlanInfo.ExpGrossQuantity.ToString("0.00"),
                预期净产量 = productionPlanInfo.ExpNetQuantity.ToString("0.00"),
                优先级 = productionPlanInfo.Priority.ToString(),
                计划状态 = productionPlanInfo.Status.GetEnumMember(),
                创建人 = productionPlanInfo.CreatorName,
                审核人 = productionPlanInfo.CheckerName,
                审核状态 = productionPlanInfo.CheckStatus.GetEnumMember(),
                审核时间 = productionPlanInfo.CheckedTime?.ToString(),
                创建时间 = productionPlanInfo.AddedTime.ToString(CommonConstants.TimeFormat)
            };

            return productionPlanEm;
        }
        #endregion

        #region # 作业计划导出模型映射 —— static ExecutionPlanEm ToExportView(this ExecutionPlanInfo...
        /// <summary>
        /// 作业计划导出模型映射
        /// </summary>
        public static ExecutionPlanEm ToExportView(this ExecutionPlanInfo executionPlanInfo)
        {
            ExecutionPlanEm executionPlanEm = new ExecutionPlanEm
            {
                计划编号 = executionPlanInfo.Number,
                计划名称 = executionPlanInfo.Name,
                产品 = executionPlanInfo.ProductInfo?.Name,
                工艺 = executionPlanInfo.ProcessInfo?.Name,
                物料清单 = executionPlanInfo.BomInfo?.Name,
                生产批号 = executionPlanInfo.ProductionBatchNo,
                工厂模型 = executionPlanInfo.FacilityInfo?.Name,
                预期毛产量 = executionPlanInfo.ExpGrossQuantity.ToString("0.00"),
                预期净产量 = executionPlanInfo.ExpNetQuantity.ToString("0.00"),
                优先级 = executionPlanInfo.Priority.ToString(),
                计划状态 = executionPlanInfo.Status.GetEnumMember(),
                创建人 = executionPlanInfo.CreatorName,
                审核人 = executionPlanInfo.CheckerName,
                审核状态 = executionPlanInfo.CheckStatus.GetEnumMember(),
                审核时间 = executionPlanInfo.CheckedTime?.ToString(),
                创建时间 = executionPlanInfo.AddedTime.ToString(CommonConstants.TimeFormat)
            };

            return executionPlanEm;
        }
        #endregion

        #region # 工单导出模型映射 —— static WorkOrderEm ToExportView(this WorkOrderInfo workOrderInfo)
        /// <summary>
        /// 工单导出模型映射
        /// </summary>
        public static WorkOrderEm ToExportView(this WorkOrderInfo workOrderInfo)
        {
            WorkOrderEm workOrderEm = new WorkOrderEm
            {
                工单编号 = workOrderInfo.Number,
                工单名称 = workOrderInfo.Name,
                作业计划 = workOrderInfo.ExecutionPlanInfo?.Name,
                生产批号 = workOrderInfo.ExecutionPlanInfo?.ProductionBatchNo,
                工厂模型 = workOrderInfo.FacilityInfo?.Name,
                工位信息 = workOrderInfo.WorkStationInfo.Name,
                分派班组 = workOrderInfo.DispatchedTeamInfo?.Name,
                分派人员 = workOrderInfo.DispatchedPersonInfo?.Name,
                实际开始时间 = workOrderInfo.ActualStartTime.ToString(),
                实际结束时间 = workOrderInfo.ActualEndTime.ToString(),
                工单状态 = workOrderInfo.Status.GetEnumMember()
            };

            if (workOrderInfo.DispatchedTeamId.HasValue)
            {
                workOrderEm.工单分派类型 = "分派班组";
            }
            if (workOrderInfo.DispatchedPersonId.HasValue)
            {
                workOrderEm.工单分派类型 = "分派人员";
            }

            return workOrderEm;
        }
        #endregion

        #region # 工单质检导出模型映射 —— static WorkOrderInspectionEm ToExportView(this WorkOrderInspectionInfo...
        /// <summary>
        /// 工单质检导出模型映射
        /// </summary>
        public static WorkOrderInspectionEm ToExportView(this WorkOrderInspectionInfo workOrderInspectionInfo)
        {
            WorkOrderInspectionEm workOrderInspectionEm = new WorkOrderInspectionEm
            {
                检验编号 = workOrderInspectionInfo.Number,
                检验名称 = workOrderInspectionInfo.Name,
                工单编号 = workOrderInspectionInfo.WorkOrderInfo?.Number,
                工单名称 = workOrderInspectionInfo.WorkOrderInfo?.Name,
                质检日期 = workOrderInspectionInfo.InspectedDate.ToString(CommonConstants.DateFormat),
                工单状态 = workOrderInspectionInfo.CheckStatus.GetEnumMember()
            };

            return workOrderInspectionEm;
        }
        #endregion

        #region # 作业计划质检导出模型映射 —— static ExecutionPlanInspectionEm ToExportView(this ExecutionPlanInspectionInfo...
        /// <summary>
        /// 作业计划质检导出模型映射
        /// </summary>
        public static ExecutionPlanInspectionEm ToExportView(this ExecutionPlanInspectionInfo executionPlanInspectionInfo)
        {
            ExecutionPlanInspectionEm executionPlanInspectionEm = new ExecutionPlanInspectionEm
            {
                检验编号 = executionPlanInspectionInfo.Number,
                检验名称 = executionPlanInspectionInfo.Name,
                计划编号 = executionPlanInspectionInfo.ExecutionPlanInfo.Number,
                计划名称 = executionPlanInspectionInfo.ExecutionPlanInfo.Name,
                质检日期 = executionPlanInspectionInfo.InspectedDate.ToString(CommonConstants.DateFormat),
                工单状态 = executionPlanInspectionInfo.CheckStatus.GetEnumMember()
            };

            return executionPlanInspectionEm;
        }
        #endregion

        #region # 设备资产导出模型映射 —— static EquipmentAssetEm ToExportView(this EquipmentAssetInfo...
        /// <summary>
        /// 设备资产导出模型映射
        /// </summary>
        public static EquipmentAssetEm ToExportView(this EquipmentAssetInfo equipmentAssetInfo)
        {
            EquipmentAssetEm equipmentAssetEm = new EquipmentAssetEm
            {
                设备编号 = equipmentAssetInfo.EquipmentInfo.Number,
                设备名称 = equipmentAssetInfo.EquipmentInfo.Name,
                资产编号 = equipmentAssetInfo.Number,
                资产名称 = equipmentAssetInfo.Name,
                采购日期 = equipmentAssetInfo.PurchasedDate.ToString(CommonConstants.DateFormat),
                制造商 = equipmentAssetInfo.Manufacturer,
                规格 = equipmentAssetInfo.Specification,
                型号 = equipmentAssetInfo.ModelNo,
                价值 = equipmentAssetInfo.Worth.ToString("0.00"),
                使用年限 = equipmentAssetInfo.ServiceLife,
                折旧年限 = equipmentAssetInfo.DepreciableLife,
                售后电话 = equipmentAssetInfo.SupportPhone,
                设备状态 = equipmentAssetInfo.Status.GetEnumMember()
            };

            return equipmentAssetEm;
        }
        #endregion

        #region # 设备维护记录导出模型映射 —— static EquipmentMaintenanceEm ToExportView(this EquipmentMaintenanceInfo...
        /// <summary>
        /// 设备维护记录导出模型映射
        /// </summary>
        public static EquipmentMaintenanceEm ToExportView(this EquipmentMaintenanceInfo equipmentMaintenanceInfo)
        {
            EquipmentMaintenanceEm equipmentMaintenanceEm = new EquipmentMaintenanceEm
            {
                记录编号 = equipmentMaintenanceInfo.Number,
                记录名称 = equipmentMaintenanceInfo.Name,
                设备维护类 = equipmentMaintenanceInfo.EquipmentMaintainClassInfo.Name,
                设备资产编号 = equipmentMaintenanceInfo.EquipmentAssetInfo.Number,
                设备资产名称 = equipmentMaintenanceInfo.EquipmentAssetInfo.Name,
                维护日期 = equipmentMaintenanceInfo.MaintainedDate.ToString(CommonConstants.DateFormat),
                维护工时 = equipmentMaintenanceInfo.MaintainedHour.HasValue
                    ? equipmentMaintenanceInfo.MaintainedHour.Value.ToString("0.00")
                    : string.Empty,
                维护部位 = equipmentMaintenanceInfo.MaintainedParts,
                维护内容 = equipmentMaintenanceInfo.MaintainedContent,
                备件使用 = equipmentMaintenanceInfo.SparePartsUse,
                维护人 = equipmentMaintenanceInfo.Maintainer,
                工厂模型 = equipmentMaintenanceInfo.FacilityInfo?.Name,
                描述 = equipmentMaintenanceInfo.Description
            };

            return equipmentMaintenanceEm;
        }
        #endregion
    }
}
